package com.jwpdk.dbframework;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.sql.Types;

import org.apache.log4j.Logger;

import com.jwpdk.appframework.data.DatasetResult;
import com.jwpdk.appframework.data.IDataList;
import com.jwpdk.util.StringUtils;
/**
 * 
 * @author 王志伟
 *
 */
public abstract class DAOBase {

	protected final Logger	log	= Logger.getLogger(this.getClass());

	public static Object getValueByResultSet(ResultSet rs, int type, String name) throws Exception {
		return type == Types.BLOB ? rs.getBlob(name) : type == Types.CLOB ? StringUtils.getStringByClob(rs.getClob(name)) : rs.getString(name);
	}

	protected static void getExecuteTimeBySql(Logger log, long beginTime) throws Exception {
		log.debug("SQL execute time: " + ((double) (System.currentTimeMillis() - beginTime) / (double) 1000) + "s");
	}

	/**
	 * 关闭 statement,resultset
	 * @param iStatement
	 * @param rs
	 * @throws Exception
	 */
	protected void close(IStatement iStatement, ResultSet rs) throws Exception {
		if ( rs != null )
			rs.close();
		iStatement.close();
	}

	/**
	 * 根据IStatement查询
	 * @param iStatement
	 * @return
	 * @throws Exception
	 */
	public IDataList queryByIStatement(IStatement iStatement) throws Exception {
		ResultSet rs = null;
		IDataList idList = null;
		try {
			rs = iStatement.executeQuery();
			idList = new DatasetResult(rs);
		} catch ( Exception e ) {
			log.error(e);
			throw e;
		} finally {
			close(iStatement,rs);
		}
		return idList;
	}

	/**
	 * 根据IStatement执行CUD
	 * @param iStatement
	 * @return
	 * @throws Exception
	 */
	public int executeByIStatement(IStatement iStatement) throws Exception {
		int result = 0;
		try {
			result = iStatement.executeUpdate();
		} catch ( Exception e ) {
			log.error(e);
			throw e;
		} finally {
			close(iStatement,null);
		}
		return result;
	}

	/**
	 * 获取数据库系统时间的SQL
	 * @return
	 */
	public abstract String getDBSysdateSql();

	/**
	 * 获取数据库系统时间的SQL
	 * @return
	 */
	public Timestamp getSysdate(Connection conn) throws Exception {
		return getBaseSysdate(conn,getDBSysdateSql());
	}

	/**
	 * 获取sql执行数
	 * @param conn
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public int getCount(Connection conn, SQL sql) throws Exception {
		IStatement iStatement = null;
		String sqlString = getCountSql(sql.getAskSql());
		if ( sql.isParamSql() ) {
			sql.setSql(sqlString,false);
			iStatement = new ParameterStatement(conn, sql);
		} else {
			iStatement = new SimpleStatement(conn, sql);
		}
		ResultSet rs = null;
		try {
			rs = iStatement.executeQuery();
			int i = rs.next() ? rs.getInt("TOTALNUM") : 0;
			log.debug("getCount()=" + i);
			return i;
		} catch ( Exception e ) {
			log.error(e);
			throw e;
		} finally {
			close(iStatement,rs);
		}
	}

	/**
	 * @param conn
	 * @param sql
	 * @param rstype 0 返回String 1 返回Timestamp
	 * @return
	 * @throws Exception
	 */
	private Timestamp getBaseSysdate(Connection conn, String sqlstr) throws Exception {
		SQL sql = new SQL(sqlstr);
		IStatement iStatement = new SimpleStatement(conn, sql);
		ResultSet rs = iStatement.executeQuery();
		Timestamp time = rs.next() ? rs.getTimestamp(1) : null;
		close(iStatement,rs);
		return time;
	}

	private String getCountSql(String sql) {
		StringBuffer sbsql = new StringBuffer();
		sbsql.append("select count(1) as totalnum from ( ").append(sql).append(" ) tmpcount_");
		return sbsql.toString();
	}

}
